<?php namespace App\Http\Controllers\Backstage;

use App\Http\Controllers\Controller;
use App\Services\ProdTempCategoryService\ProdTempCategoryBackService;
use App\Services\Tool\GeneralTool;
use Illuminate\Http\Request;
use App\Services\SizeService\BackstageSizeService as SizeService;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Response;

class SizeController extends Controller
{

    protected $sizeService;
    protected $categoryBackService;

    public function __construct(SizeService $sizeService,ProdTempCategoryBackService $categoryBackService)
    {
        $this->sizeService = $sizeService;
        $this->categoryBackService = $categoryBackService;
    }

    /**
     * 获取尺码列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function getSizeList(Request $request)
    {
        $class_id=intval($request->input('class_id'));
        if(!empty($class_id)&&!is_int($class_id)){
            return redirect()->back()->with('error_message','id错误');
        }
        $sizes=$this->sizeService->getSizeByProdClassId($class_id);
        if(empty($sizes)){
            return redirect()->back()->with('error_message','尺码不存在');
        }
        $temp_categorys=$this->categoryBackService->getAllProdTempCategoryList();
        return view('backstage.size.sizeList')->with('sizes',$sizes)
            ->with('temp_categorys',$temp_categorys);
    }

    /**
     * 获取尺码列表
     * @param Request $request
     * @return mixed
     */
    public function postGetSizeList(Request $request)
    {
        $class_id=intval($request->input('class_id'));
        if(!empty($class_id)&&!is_int($class_id)){
            $result["state"] = -3;
            $result["message"] = "id为空";
            $result["data"] = array();;
        }
        $sizes=$this->sizeService->getSizeByProdClassId($class_id);
        if(empty($sizes)){
            $result["state"] = -1;
            $result["message"] = "失败";
            $result["data"] = array();
        }else{
            $result["state"] = 1;
            $result["message"] = "成功";
            $result["data"] = $sizes;
        }
        return response()->json($result);
    }

    /**
     * 添加尺码页面
     * @return $this
     */
    public function addSizePage()
    {
        $temp_categorys=$this->categoryBackService->getAllProdTempCategoryList();
        return view('backstage.size.addSize')->with('temp_categorys',$temp_categorys);
    }
    /**
     * 获取尺码列表
     * @param Request $request
     * @return mixed
     */
    public function postJsonSizeList(Request $request){
        $sizes =  $this->sizeService->getJsonSizeList($request);
        if(empty($sizes)){
            $result["state"] = -1;
            $result["message"] = "失败";
            $result["data"] = array();
        }else{
            $result["state"] = 1;
            $result["message"] = "成功";
            $result["data"] = $sizes;
        }
        return response()->json($result);
    }

    /**
     * 保存尺码
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function postAddSize(Request $request)
    {
        $size_name = trim($request->input('size_name'));
        $prod_class_id= intval($request->input('prod_class_id'));
        $size =  array(
            'size_name'=>$size_name,
            'prod_class_id'=>$prod_class_id,
        );
        $validator =  $this->sizeService->validator($size);
        $validator->after(function($validator) use ($size_name,$prod_class_id)
        {
            //判断除当前尺码名称外有没有重复尺码名称
            $b =  $this->sizeService->getSizeNameExistByProdClassId($size_name,$prod_class_id);
            if(!empty($b))
            {
                $validator->errors()->add('size_name', '尺码名称已存在!');
            }
            $prod_class_exist =  $this->categoryBackService->prodCategoryExistById($prod_class_id);
            if(!$prod_class_exist)
            {
                $validator->errors()->add('prod_class_id', '产品分类不存在!');
            }
        });
        if ($validator->fails())
        {
            $request->flash();
            return redirect()->back()->withErrors($validator->errors())->withInput();;
        }
        $size['size_name']=strtoupper($size_name);
        $res=$this->sizeService->addSize($size);
        if(!$res){
            return redirect()->back()->with('error_message','添加失败');
        }
        return redirect()->back()->with('success_message','添加成功');
    }
}
